import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:sainbainu/common/services/app_update.dart';
import 'package:sainbainu/common/utils/loading.dart';
import 'package:sainbainu/common/values/colors.dart';

class AppUpdateController {
  late void Function() onShowUpdate;
}

class AppUpdateDialog extends GetView<AppUpdateService> {
  final Widget child;
  final AppUpdateController apController;

  AppUpdateDialog({
    super.key,
    required this.child,
    required this.apController,
  }) {
    apController.onShowUpdate = onShowUpdate;
  }

  /// 显示更新弹窗
  void onShowUpdate() {
    showGeneralDialog(
      context: Get.context!,
      barrierColor: const Color.fromRGBO(0, 0, 0, 0.5),
      pageBuilder: (_, a, b) => _buildDialog(),
    );
  }

  /// 绘制弹窗
  Widget _buildDialog() {
    return Center(
      child: Container(
        width: 280,
        padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 15),
        decoration: const BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.all(Radius.circular(15)),
        ),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          mainAxisSize: MainAxisSize.min,
          children: [
            _buildTitle(),
            const SizedBox(height: 10),
            _buildContent(),
            const SizedBox(height: 20),
            _buildBtns(),
          ],
        ),
      ),
    );
  }

  /// 弹窗标题
  Widget _buildTitle() {
    return const SizedBox(
      width: double.infinity,
      child: Text(
        '新版本发布',
        textAlign: TextAlign.center,
        style: TextStyle(
          fontSize: 17,
          fontWeight: FontWeight.w600,
          color: AppColors.active,
        ),
      ),
    );
  }

  Widget _buildContent() {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Obx(
          () => Text(
            '是否升级到${controller.versionUpdateResponse?.value.versionName}版本？',
            style: const TextStyle(fontSize: 15),
          ),
        ),
        const SizedBox(height: 10),
        Obx(() {
          if (controller.versionUpdateResponse == null) {
            return const SizedBox.shrink();
          }
          if (controller.versionUpdateResponse!.value.updateContentList ==
              null) {
            return const SizedBox.shrink();
          }
          return Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: controller.versionUpdateResponse!.value.updateContentList!
                .map(
                  (t) => Text(
                    t,
                    style: const TextStyle(
                      fontSize: 14,
                      color: AppColors.active2,
                    ),
                  ),
                )
                .toList(),
          );
        }),
      ],
    );
  }

  Widget _buildBtns() {
    return Row(
      children: [
        TextButton(
          onPressed: () => controller.cancelUpdate(),
          style: TextButton.styleFrom(
            foregroundColor: AppColors.unactive,
          ),
          child: const Text('残忍拒绝'),
        ),
        const SizedBox(width: 10),
        Expanded(
          child: ElevatedButton(
            onPressed: () => controller.confirmUpdate(),
            style: ElevatedButton.styleFrom(
              backgroundColor: AppColors.danger,
              foregroundColor: Colors.white,
              enableFeedback: true,
            ),
            child: const Text('立即体验'),
          ),
        ),
      ],
    );
  }

  @override
  Widget build(BuildContext context) => child;
}
